package com.sbw.front.demo.remote;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author sangbowei
 * @date 2018年7月9日
 *  httpClient模板类,项目启动后直接autowired使用,无需new该对象
 *  
 */
public class HttpClient {

	@Autowired
    private CloseableHttpClient httpClient;

    @Autowired
    private RequestConfig config;

    //不带参数的get请求
    public String doGet(String url) throws Exception {
        
    	//声明 http get请求
        HttpGet httpGet = new HttpGet(url);
        //装载配置信息
        httpGet.setConfig(config);
        //发起请求
        CloseableHttpResponse response = httpClient.execute(httpGet);
        //返回响应体的内容
        return EntityUtils.toString(response.getEntity(), "UTF-8");
    }

    //带参数的get请求
    public String doGet(String url, Map<String, Object> map)throws Exception {
        
    	URIBuilder uriBuilder = new URIBuilder(url);
        if (map != null && !map.isEmpty()) {
            //遍历map,拼接请求参数
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        //调用不带参数的get请求
        return this.doGet(uriBuilder.build().toString());
    }
    
    //post请求(from表单形式)
    public String doPost(String url, Map<String, Object> map) throws Exception {
    	
    	//创建http POST请求
    	HttpPost httpPost = new HttpPost(url);
    	//装载配置信息
        httpPost.setConfig(config);
        //判断map是否为空,不为空则进行遍历,封装from表单对象
    	if (map != null && !map.isEmpty()) {
    		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    		//遍历map,拼接请求参数
    		for (Map.Entry<String, Object> entry : map.entrySet()) {
    			parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
    		}
    		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");
    		httpPost.setEntity(formEntity);
    	}
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //返回响应体的内容
        return EntityUtils.toString(response.getEntity(), "UTF-8");
    }
    
    //post请求(json形式)
    public String doPost(String url, String json) throws Exception {
    	
    	//创建http POST请求
    	HttpPost httpPost = new HttpPost(url);
    	//装载配置信息
        httpPost.setConfig(config);
        //判断json是否为空
        if (StringUtils.isNotBlank(json)) {
        	StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
        	entity.setContentEncoding("UTF-8");
        	entity.setContentType("application/json");
        	httpPost.setEntity(entity);
		}
        CloseableHttpResponse response = httpClient.execute(httpPost);
    	//返回响应体的内容
        return EntityUtils.toString(response.getEntity(), "UTF-8");
    }
    
}
